package cn.tedu.charging.order.quartz;

import cn.tedu.charging.commen.JsonResult;
import cn.tedu.charging.commen.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.order.common.GunStatusConstants;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.pojo.po.ChargingOrderFailPO;
import cn.tedu.charging.order.pojo.po.ChargingOrderSuccessPO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.UUID;

@Slf4j
public class DeviceQuartzJob implements Job {

    public DeviceQuartzJob() {
    }

    public DeviceQuartzJob(String orderNo,Integer gunId) throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();

        Scheduler scheduler = schedulerFactory.getScheduler();

        JobDetail jobDetail = JobBuilder.newJob(DeviceQuartzJob.class)
                .withIdentity("device-check-job" + UUID.randomUUID(),"device-check-job-group")
                .usingJobData("orderNo",orderNo)
                .usingJobData("gunId",gunId)
                .build();

        Long now = System.currentTimeMillis();
        Long  deviceCheckTime = now + 60 * 1000;
        Date triggerTime = new Date(deviceCheckTime);
        Trigger trigger = TriggerBuilder.newTrigger()
                .startAt(triggerTime)
                .build();

        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.start();
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.debug("设备自检完成！");
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String orderNo = jobDataMap.getString("orderNo");
        Integer gunId = jobDataMap.getInt("gunId");
        OrderRepository orderRepository = SpringContextUtils.getBean(OrderRepository.class);
        DeviceClient deviceClient = SpringContextUtils.getBean(DeviceClient.class);
        ChargingOrderSuccessPO orderSuccessPO = orderRepository.getSuccessOrder(orderNo);
        if (orderSuccessPO == null){//没有成功订单
            ChargingOrderFailPO orderFailPO = orderRepository.getFailOrder(orderNo);
            if (orderFailPO == null){//也没有失败订单
                //插入失败订单
                ChargingOrderFailPO chargingOrderFailPO = new ChargingOrderFailPO();
                chargingOrderFailPO.setBillId(orderNo);
                chargingOrderFailPO.setGunId(gunId);
                chargingOrderFailPO.setFail_desc("设备自检失败，设备无响应！");
                log.debug("向数据库插入失败订单信息，数据:{}",chargingOrderFailPO);
                Integer row = orderRepository.saveFailOrder(chargingOrderFailPO);

                //同时将枪状态修改为 离线
                GunStatusUpdateParam gunStatusUpdateParam = new GunStatusUpdateParam();
                gunStatusUpdateParam.setGunId(gunId);
                gunStatusUpdateParam.setStatus(GunStatusConstants.GUN_STATUS_OFFLINE);
                log.debug("修改枪状态，参数:{}",gunStatusUpdateParam);
                JsonResult<Boolean> booleanJsonResult = deviceClient.updateGunStatus(gunStatusUpdateParam);
                log.debug("修改枪状态参数:{},结果:{}",gunStatusUpdateParam,booleanJsonResult);
                log.debug("通知用户，通知维修人员");
            }else {
                log.debug("没有成功订单，有失败订单，无需处理");
            }
        }else {
            log.debug("有成功订单，无需处理");
        }
    }
}
